import 'package:flutter/material.dart';
import 'package:xws/helpers/safe_state.dart';
import 'package:xws/models/user/user_obj.dart';
import 'package:xws/utils/dio_utils/request/user_req.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/utils/tools_util.dart';
import 'package:xws/widgets/app_text_field.dart';
import 'package:xws/widgets/refresh_widget/refresh_load_view.dart';
import 'package:xws/widgets/safe_appbar.dart';
import 'package:xws/widgets/safe_scaffold.dart';

import 'friedn_item.dart';

/// 搜索用户
class SearchUser extends StatefulWidget {
  const SearchUser({Key? key, this.refresh}) : super(key: key);

  @override
  _SearchUserState createState() => _SearchUserState();

  final Future<void> Function()? refresh;
}

class _SearchUserState extends State<SearchUser> with SafeState<SearchUser> {
  late RefreshViewController<User> _viewController;
  late FocusNode _focusNode;

  String _key = '';

  bool get _isKeyEmpty => _key.trim().isEmpty;

  @override
  void initState() {
    super.initState();
    _viewController = RefreshViewController<User>();
    _focusNode = FocusNode();
    delayed(const Duration(milliseconds: 400)).then((_) => _focusNode.requestFocus());
  }

  @override
  void dispose() {
    _viewController.dispose();
    _focusNode.dispose();
    super.dispose();
  }

  /// 搜索框值变化
  void _onChanged(String v) {
    if (v.isNotEmpty) {
      if (_isKeyEmpty) {
        safeSetState(() => _key = v);
      } else {
        _key = v;
      }

      _viewController.refresh();
    } else {
      if (!_isKeyEmpty) {
        safeSetState(() => _key = '');
        _viewController.refresh();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeScaffold(
      appBar: SafeAppBar(
        title: Padding(
          padding: const EdgeInsets.symmetric(vertical: 2),
          child: AppTextField(
            focusNode: _focusNode,
            hintText: '搜索用户...',
            onChanged: _onChanged,
            maxLength: 16,
            border: OutlineInputBorder(
              borderSide: BorderSide.none,
              borderRadius: BorderRadius.circular(6),
            ),
            fillColor: canvas(context),
            contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
          ),
        ),
      ),
      body: _buildList,
    );
  }

  Widget get _buildList {
    return Offstage(
      offstage: _isKeyEmpty,
      child: ColoredBox(
        color: white(context),
        child: RefreshLoadView<User>(
          viewController: _viewController,
          onRefresh: (int v, _) async {
            if (_isKeyEmpty) return <User>[];
            return await searchUser(keyword: _key, pageNum: 1);
          },
          onLoad: (int page, _) async {
            if (_isKeyEmpty) return <User>[];
            return await searchUser(keyword: _key, pageNum: page);
          },
          itemBuilder: (int index, User u) => FriendItem(
            user: u,
            isSearch: true,
            refresh: widget.refresh,
          ),
        ),
      ),
    );
  }
}
